package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.Nearby;
import com.ruoyi.sj.domain.PersonalData;
import com.ruoyi.sj.mapper.NearbyMapper;
import com.ruoyi.sj.service.IPersonalDataService;
import com.ruoyi.web.controller.util.LongLatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AppNearbyService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/13 0013 14:16
 **/
@Service
public class AppNearbyService {
    @Autowired
    private NearbyMapper nearbyMapper;
    @Autowired
    private IPersonalDataService iPersonalDataService;


    private static final double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }


    /**
     * 存储用户登录时经纬度
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return
     */
    public Object insertNearby(String longitude, String latitude, String memberId,Integer type) {
        if (StringUtils.isBlank(longitude)) {
            throw new XException("经度不能为空");
        }
        if (StringUtils.isBlank(latitude)) {
            throw new XException("纬度不能为空");
        }
        if (StringUtils.isBlank(memberId)) {
            throw new XException("用户ID号不能为空");
        }
        if (type==null){
            throw new XException("类型不能为空");
        }
        Nearby nearby = new Nearby();
        nearby.setMemberId(Long.valueOf(memberId));
        nearby.setLatitude(latitude);
        nearby.setLongitude(longitude);
        nearby.setType(type);
        return nearbyMapper.insertNearby(nearby);
    }

    /**
     * 查询是否存储过经纬度
     *
     * @param openid
     * @return
     */
    public Nearby queryByMemberId(String openid) {
        return nearbyMapper.queryById(openid);
    }

    /**
     * 修改用户经纬度
     *
     * @param longitude
     * @param latitude
     * @param memberId
     * @return
     */
    public int updateNearby(String longitude, String latitude, String memberId) {
        if (StringUtils.isBlank(memberId)) {
            throw new XException("用户ID不能为空");
        }
        Nearby nearby = new Nearby();
        nearby.setLongitude(longitude);
        nearby.setLatitude(latitude);
        nearby.setMemberId(Long.valueOf(memberId));
        return nearbyMapper.update(nearby);
    }

    /**
     * 附近
     *
     * @param map
     * @return
     */
    public Object queryUsers(Map<String, String> map) {
        Double longitude = Double.valueOf(map.get("longitude")); //中心点经度
        Double latitude = Double.valueOf(map.get("latitude"));  //中心点纬度
        int radius = 2000;  //设置半径单位（米）
        Double[] aroud = LongLatUtils.getAroud(longitude, latitude, radius);
        Double minLat = aroud[0];  //最小纬度
        Double minLng = aroud[1];   //最小经度
        Double maxLat = aroud[2];  //最大纬度
        Double maxLng = aroud[3];  //最大经度
        List<Nearby> nearbyList = nearbyMapper.queryUserList(minLat, minLng, maxLat, maxLng);//查询此范围内用户
        if (nearbyList == null) {
            return nearbyList;
        } else {
            List list = new ArrayList();
            for (Nearby nearby : nearbyList) {
                Long memberId = nearby.getMemberId(); //取出查询到的用户ID
                Double lat = Double.valueOf(nearby.getLatitude());   //目标纬度
                Double log = Double.valueOf(nearby.getLongitude());//目标经度
                double distance = GetDistance(longitude, latitude, log, lat); //得到两点间距离
                PersonalData personalData = iPersonalDataService.selectPersonalDateByMemberId(memberId); //查询闺蜜圈中个人资料信息
                if (personalData == null) {
                    throw new XException("此用户未设置闺蜜圈");
                }
                personalData.setDistance(distance);
                list.add(personalData);
            }
            return list;
        }

    }

    /**
     * 基于googleMap中的算法得到两经纬度之间的距离,计算精度与谷歌地图的距离精度差不多，相差范围在0.2米以下
     *
     * @param lon1 中心点的精度
     * @param lat1 中心点的纬度
     * @param lon2 目标点的精度
     * @param lat3 目标点的纬度
     * @return 返回的距离，单位m
     */
    public static double GetDistance(double lon1, double lat1, double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

   /* public static void main(String[] args) {
        double lon1 = 109.0145193757;
        double lat1 = 34.236080797698;
        double lon2 = 108.9644583556;
        double lat2 = 34.286439088548;
        int radius = 2000;
        double distance = GetDistance(lon1, lat1, lon2, lat2);
        System.out.println(distance);

        Double[] aroud = LongLatUtils.getAroud(106.497921, 29.614706, 2000);
        System.out.println("====================================");
        System.out.println(aroud[0] + "+++++++++" + aroud[1] + "+++++++" + aroud[2] + "++++++++" + aroud[3]);
    }*/
}
